package com.sensologyhome_all.activity;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.sensologyhome_all.R;
import com.sensologyhome_all.util.DialogUtil;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionNo;
import com.yanzhenjie.permission.PermissionYes;

import java.util.List;

/**
 * Created by hogan on 2017/8/17.
 */

public class WelcomeActivity extends Activity implements Animation.AnimationListener {
    private AlphaAnimation mAlphaAnim;
    View mContentView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_act_welcom);
        initView();
        requestPermit();
    }

    private void initView() {
        mContentView=findViewById(R.id.iv_welcom);
        mAlphaAnim = (AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.welcome);
        mAlphaAnim.setAnimationListener(this);

    }

    private void initEvent() {
        if (mContentView != null) {
            mContentView.setAnimation(mAlphaAnim);
            mContentView.startAnimation(mAlphaAnim);
        }
    }

    private void requestPermit() {
        AndPermission.with(this)
                .requestCode(100)
                .permission(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION)
                .send();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        AndPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
//        AndPermission.onRequestPermissionsResult(requestCode, permissions, grantResults, listener);
    }

    // 成功回调的方法，用注解即可，里面的数字是请求时的requestCode。
    @PermissionYes(100)
    private void getLocationYes(List<String> grantedPermissions) {
        // TODO 申请权限成功。
        for (String ss : grantedPermissions) {
            Log.i("permit", ss);
        }
        initEvent();
    }

    // 失败回调的方法，用注解即可，里面的数字是请求时的requestCode。
    @PermissionNo(100)
    private void getLocationNo(List<String> deniedPermissions) {
        // 用户否勾选了不再提示并且拒绝了权限，那么提示用户到设置中授权。
        if (AndPermission.hasAlwaysDeniedPermission(this, deniedPermissions)) {
            // 第一种：用默认的提示语。
            requestPermissionDeniedDialog();
        } else {
            requestPermissionDialog();
        }
    }

    private void requestPermissionDialog() {
        AlertDialog dialog = DialogUtil.createAlertDialog(this, R.string.init_permission_prompt);
        dialog.show();
        dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestPermit();
            }
        });
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void requestPermissionDeniedDialog() {
        AlertDialog dialog = DialogUtil.createAlertDialog(this, R.string.init_permission_denied_prompt);
        dialog.show();
        dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                startActivity(intent);
            }
        });
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Override
    public void onAnimationEnd(Animation animation) {
        Log.i("sss", "onAnimationEnd:  " + (animation.hashCode() == mAlphaAnim.hashCode()));
        if (animation.hashCode() == mAlphaAnim.hashCode()) {
//            GlobalValue.mPointWidth = imgAirsensorSlidpoint1.getWidth();
//            Intent intent = new Intent(WelcomeActivity.this, ADActivity.class);
//            startActivity(intent);
//            finish();
        }
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub
    }


}
